<?php
//模型
class UserCardModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user_card';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){		
		$where = ' where 1 ';//" where uc.num >0  ";
		if($arr['user_id']){
			$where .= " AND uc.user_id='{$arr['user_id']}' ";
		}
		
		$sql  = "select uc.*, c.name as course_name, c.theme_img, c.type_id, c.duration, m.nickname as coach_name, s.name as shop_name, cc.card_name from 
				".$this->db->dbprefix.$this->table." as uc left join 
				".$this->db->dbprefix."course as c on uc.cid=c.id left join 
				".$this->db->dbprefix."coach as m on c.cid=m.id left join
				".$this->db->dbprefix."shop as s on c.shop_id=s.id left join
				".$this->db->dbprefix."course_card as cc on cc.id=uc.card_id 
				
				{$where} LIMIT ".($page-1)*$size .",{$size}";
		
		$query = $this->db->query($sql);
		$res = $query->result_array();		
		
		foreach($res as $k=>$v){
			if($v['type_id']==0){
				$res[$k]['type_name'] = '私教';
			}else{
				$res[$k]['type_name'] = '团教';
			}
			if(empty($v['card_name'])){
				$res[$k]['card_name'] = '';
			}
		}
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." as uc {$where} ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	
	//全部列表
	function get_lists($user_id=0){
		$where = ' where 1 ';//" where uc.num >0  ";
		if($user_id){
			$where .= " and user_id='{$user_id}' ";
		}
		
		$sql = "select uc.*, c.name as course_name, c.theme_img, c.type_id from 
			   ".$this->db->dbprefix.$this->table." as uc left join 
			   ".$this->db->dbprefix."course as c on uc.cid=c.id 
			   {$where} ";

		$query = $this->db->query($sql);
		$res = $query->result_array();
		
		foreach($res as $k=>$v){
			if($v['type_id']==0){
				$res[$k]['type_name'] = '私教';
			}else{
				$res[$k]['type_name'] = '团教';
			}
			if(empty($v['card_name'])){
				$res[$k]['card_name'] = '';
			}
		}
		
		return $res;		
	}
	//详情
	function get_info($id){
		$sql = "select uc.*, c.name as course_name, c.theme_img, c.type_id, c.cid as coach_id, c.shop_id, c.duration, c.max_num from 
			   ".$this->db->dbprefix.$this->table." as uc left join 
			   ".$this->db->dbprefix."course as c on uc.cid=c.id  where uc.id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
}

